﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Airport;
using System.Runtime.Serialization;

namespace Airport.AirportDataTypes
{
    [DataContract(IsReference = true)]
    [KnownType(typeof(HoldMitigation))]
    [KnownType(typeof(DelayStartMitigation))]
    [KnownType(typeof(CancelTakeoffMitigation))]
    public abstract class Mitigation
    {
        //private MitigationType _type;
        protected Plane _mitigationPlane;
        protected string _instruction;
        protected Route _mitigatedRoute;

        public Mitigation()
            : this(null)
        {
        }
        
        public Mitigation(Plane mitigationPlane)
            : this(mitigationPlane, null)
        {
        }
        
        
        public Mitigation(Plane mitigationPlane, Route mitigationRoute)
        {
            //_type = MitigationType.Uninitialized;
            _mitigationPlane = mitigationPlane;
            _instruction = "";
            _mitigatedRoute = mitigationRoute;
        }
        
        /*
        [DataMember]
        public MitigationType Type
        {
            get
            {
                return _type;
            }

            set
            {
                _type = value;
            }
        }
        */
        
        [DataMember]
        public Plane MitigationPlane
        {
            get
            {
                return _mitigationPlane;
            }

            set
            {
                _mitigationPlane = value;
            }
        }
        
        [DataMember]
        public string Instruction
        {
            get
            {
                return _instruction;
            }

            set
            {
                _instruction = value;
            }
        }
        
        [DataMember]
        public Route MitigatedRoute
        {
            get
            {
                return _mitigatedRoute;
            }

            set
            {
                _mitigatedRoute = value;
            }
        }
        
    }
    /*
    [DataContract]
    public enum MitigationType
    {
        [EnumMember]
        Uninitialized,
        [EnumMember]
        Hold,
        [EnumMember]
        DelayStart
    }
    */
    [DataContract(IsReference = true)]
    public class HoldMitigation : Mitigation
    {
        private string _holdIntersection;
        
        public HoldMitigation(Plane mitigationPlane, Route mitigationRoute)
            : base(mitigationPlane, mitigationRoute)
        {
            _holdIntersection = "";
        }

        [DataMember]
        public string HoldIntersection
        {
            get
            {
                return _holdIntersection;
            }

            set
            {
                _holdIntersection = value;

                string instruction = _mitigationPlane.Name;
                instruction += " Hold Short of " + _holdIntersection;
                _instruction = instruction;
            }
        }
    }

    [DataContract(IsReference = true)]
    public class DelayStartMitigation : Mitigation
    {
        private DateTime _delayedStartTime;

        public DelayStartMitigation(Plane mitigationPlane, Route mitigationRoute)
            : base(mitigationPlane, mitigationRoute)
        {
            if (mitigationRoute != null)
            {
                DelayedStartTime = mitigationRoute.StartTime;
            }
            else
            {
                DelayedStartTime = new DateTime();
            }
            
        }

        [DataMember]
        public DateTime DelayedStartTime
        {
            get
            {
                return _delayedStartTime;
            }

            set
            {
                _delayedStartTime = value;

                string instruction = _mitigationPlane.Name;
                instruction += " Delay Start Until " + _delayedStartTime.ToString("HH:mm:ss");
                _instruction = instruction;
            }
        }
    }

    [DataContract(IsReference = true)]
    public class CancelTakeoffMitigation : Mitigation
    {
        public CancelTakeoffMitigation(Plane mitigationPlane)
            : base(mitigationPlane)
        {
            _instruction = _mitigationPlane.Name + " Cancel Takeoff Clearance";
        }
    }
}